#pragma once
#pragma once
#include <unordered_map>
#include <atomic>
#include <Windows.h>
#include <windows.h>
#include <thread>
#include<iostream>
#include<string> 
#include <Python.h>
inline unsigned int getThreadId()
{
	std::thread::id this_id = std::this_thread::get_id();
	return *(unsigned int*)&this_id;
}
class threadlock {
public:
	threadlock(PyObject* a) {
		this->a = a;
		load();
	};
	PyObject* get() {
		while (true) {
			if (*getbool != true) {
				*getbool = true;
				*haveid = thisid;
				break;
			}
			if (*haveid == thisid) {
				break;
			}
		}
		return this->a;
	}
	threadlock operator =(const threadlock& other) {
		this->getbool = other.getbool;
		this->a = other.a;
		this->haveid = other.haveid;
		this->thisid = getThreadId();
		return *(this);
	}
	~threadlock() {
		*getbool = false;
	}
private:
	PyObject* a;
	std::atomic<bool>* getbool;
	std::atomic<long long>* haveid;
	unsigned long long thisid = getThreadId();
	void load() {
		getbool = new std::atomic<bool>;
		*getbool = false;
		haveid = new std::atomic<long long>;
		haveid->store(-1);
	}
};
